package com.example.mvp.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;

import com.example.inter.RecyclerViewRefreshInterface;
import com.example.inter.RefreshLoadMoreInferface;

/**
 * 自带刷新的RecyclerView
 * @author chaohaipeng  on 2018/8/6.
 */

public class RefreshRecyclerView extends RecyclerView implements RecyclerViewRefreshInterface {
    /**
     * 当前是刷新 false-否 true-是
     */
    private boolean isRefresh = false;
    /**
     * 当前页数
     */
    private int currentPage = 1;
    /**
     * 服务器返回的总页数
     */
    private int totalPage;
    /**
     * 总的数据条数
     */
    private int count;
    private RefreshLoadMoreInferface refreshLoadMoreInferface;
    /**
     * 当前RecyclerView类型
     */
    protected LayoutManagerType layoutManagerType;

    /**
     * 最后一个的位置
     */
    private int[] lastPositions;



    /**
     * 当前滑动的状态
     */
    private int currentScrollState = 0;
    private BaseRecyclerViewAdapter adapter;
    private RecyclerViewRefreshInterface listViewRefreshInterface;
    public RefreshRecyclerView(Context context) {
        super(context);
        initEvent();
    }

    public RefreshRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initEvent();
    }

    public RefreshRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initEvent();
    }


    public RefreshLoadMoreInferface getRefreshLoadMoreInterface() {
        return refreshLoadMoreInferface;
    }

    public void setRefreshLoadMoreInterface(RefreshLoadMoreInferface refreshLoadMoreInferface) {
        this.refreshLoadMoreInferface = refreshLoadMoreInferface;
    }


    public RecyclerViewRefreshInterface getListViewRefreshInterface() {
        return listViewRefreshInterface;
    }

    public void setListViewRefreshInterface(RecyclerViewRefreshInterface listViewRefreshInterface) {
        this.listViewRefreshInterface = listViewRefreshInterface;
    }
   private void initEvent(){
       addOnScrollListener(new RecyclerView.OnScrollListener(){
           //这个int用来记录最后一个可见的view
           int lastVisibleItemPosition;

           @Override
           public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
               super.onScrollStateChanged(recyclerView, newState);
               RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
               int visibleItemCount = layoutManager.getChildCount();
               int totalItemCount = layoutManager.getItemCount();
               if (visibleItemCount > 0 && currentScrollState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition >= (totalItemCount - 1)) {
                   if (refreshLoadMoreInferface==null||adapter==null){
                       return;
                   }
                   if (currentPage >= totalPage){
                       adapter.InVisibleFootView();
                   }else{
                       adapter.VisibleFootView();
                       refreshLoadMoreInferface.loadMoreData();
                   }

               }

           }

           @Override
           public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
               super.onScrolled(recyclerView, dx, dy);
               RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
               if (layoutManagerType == null) {
                   if (layoutManager instanceof LinearLayoutManager) {
                       layoutManagerType = LayoutManagerType.LinearLayout;
                   } else if (layoutManager instanceof GridLayoutManager) {
                       layoutManagerType = LayoutManagerType.GridLayout;
                   } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                       layoutManagerType = LayoutManagerType.StaggeredGridLayout;
                   } else {
                       throw new RuntimeException(
                               "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
                   }
               }

               switch (layoutManagerType) {
                   case LinearLayout:
                       lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                       break;
                   case GridLayout:
                       lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
                       break;
                   case StaggeredGridLayout:
                       StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                       if (lastPositions == null) {
                           lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                       }
                       staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
                       lastVisibleItemPosition = findMax(lastPositions);
                       break;
               }
           }

       });



   }

    @Override
    public void loadMore(BaseRecyclerViewAdapter adapter, int current, int totalPage, int count) {
       if (adapter==null){
           return;
       }
        this.adapter=adapter;
        this.currentPage=current;
        this.totalPage=totalPage;
        this.count=count;
        isRefresh=false;
        adapter.InVisibleFootView();
    }

    /**
     * 取数组中最大值
     *
     * @param lastPositions
     * @return
     */
    private int findMax(int[] lastPositions) {
        int max = lastPositions[0];
        for (int value : lastPositions) {
            if (value > max) {
                max = value;
            }
        }

        return max;
    }

    public static enum LayoutManagerType {
        LinearLayout,
        StaggeredGridLayout,
        GridLayout
    }
}
